const mssql = require('mssql');
const fs = require("fs-extra");
const path = require("path");
module.exports = {
    async Transaction(sqlList) {
        const transaction = new mssql.Transaction(/* [pool] */);
        await transaction.begin();
        let request = new mssql.Request(transaction);
        let errItem = [];
        for (item of sqlList) {
            await request.query(item).then(result => {
            }, err => {
                errItem.push(item);
            })
        }
        if (errItem.length == 0) {
            //没有失败的语句
            transaction.commit();
            console.log("事务全部执行完成");
            return true;
        } else {
            fs.ensureDirSync(path.join(__dirname, `../log/`));
            fs.writeFileSync(path.join(__dirname, `../log/sqlerr.txt`), errItem.join('\n'));
            console.log("事务操作以回滚错误详见sqlerr.txt");
            transaction.rollback();
            return false;
        }

    },
}